home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 July / macformat-026.iso / mac / Shareware City / Developers / video-toolbox-95-01-14-c / VideoToolbox / Demos / FlickeringGrating.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-11  |  7.3 KB  |  237 lines  |  [TEXT/KAHL]

  1. /*
  2. FlickeringGrating.c
  3. This demo displays a research-grade visual stimulus. If you just want to know
  4. how to load the clut and display a pattern, then you should start by reading
  5. Grating.c, which is much shorter and simpler.
  6. Copyright (c) 1989-1993 Denis G. Pelli
  7. HISTORY:
  8. 11/89     Lan & Denis wrote it.
  9. 23.1.90    dgp        Use second screen only if available.
  10. 4/9/90    dgp        Changed WindowPtr to CWindowPtr. Made big arrays static. Reduced
  11.                 default memory allocation to 1 megabyte. Use any 8-bit screen,
  12.                 preferably not the main screen.
  13. 4/23/90    dgp        Added optional timing. Centered the image. Left clut entries 0 and 
  14.                 clutSize-1 alone, so background doesn't flash. Asked if ISR Video 
  15.                 Attenuator is present.
  16. 10/11/90 dgp    Added fpu test.
  17. 10/29/90 dgp    Added CenterRectInRect().
  18. 10/30/90 dgp    Changed call to SetLuminances() to SetLuminancesAndRange() so that
  19.                 the range is now kept fixed throughout all the frames, to avoid
  20.                 flashes.
  21. 8/24/91    dgp        Made compatible with THINK C 5.0.
  22.                 If possible, use ReadLuminanceRecord().
  23. 3/10/92    dgp        include mc68881.h
  24. 4/26/92    dgp        RestoreCluts().
  25. 8/27/92    dgp        replace SysEnvirons() by Gestalt()
  26. 12/30/92 dgp    made more like Filter, using a small console so that, if necessary,
  27.                 both the console and the grating will fit on the main monitor.
  28. 2/7/93    dgp        replaced SetOnePixel by SetPixelsQuickly
  29. 7/7/93    dgp        added code for compatibility with Radius PowerView, a SCSI video box.
  30. 9/5/94 dgp removed assumption in printf's that int==short.
  31. 11/17/94 dgp Josh Solomon reported a stack overflow, so I added a calls to StackGrow
  32. and assert.
  33. 1/11/95    dgp    Tidied up various things. Now switch to color mode if using an ISR Attenuator.
  34. */
  35. #include "VideoToolbox.h"
  36. #include "Luminance.h"
  37. #include <math.h>
  38. #include <assert.h>
  39. #if (THINK_C || THINK_CPLUS)
  40.     #include <console.h>
  41.     #include <profile.h>
  42.     #define PROFILE        0            // optionally, report timing
  43. #endif
  44. #if __MWERKS__
  45.     #include <SIOUX.h>
  46. #endif
  47. #if UNIVERSAL_HEADERS
  48.     #include <LowMem.h>
  49. #else
  50.     #define LMGetMBarHeight() (* (short *) 0x0BAA)
  51.     #define LMSetMBarHeight(MBarHeightValue) ((* (short *) 0x0BAA) = (MBarHeightValue))
  52. #endif
  53.  
  54. #define SIZE         300            // size of grating, in pixels
  55. #define TMAX        3*67        // duration, in frames, typically at 67 Hz
  56. #define REPETITIONS    5            // Number of times to repeat the animation
  57.  
  58. void main(void);
  59. void FlickeringGrating(void);
  60. typedef struct{
  61.     ColorSpec table[256];
  62. }ColorSpecTable;
  63.  
  64. void main(void)
  65. {
  66.     StackGrow(40000L+2L*SIZE*sizeof(double)+sizeof(LuminanceRecord)+TMAX*sizeof(Ptr));
  67.     Require(gestalt8BitQD);
  68.     FlickeringGrating();
  69. }
  70.     
  71. void FlickeringGrating(void)
  72. {
  73.     register int i;
  74.     int j,tmax,pixelSize,clutSize,error;
  75.     double fX[SIZE],fY[SIZE];
  76.     double LMid,LMin,LMax,dL,a,contrast;
  77.     CWindowPtr window=NULL;
  78.     WindowPtr oldPort=NULL;
  79.     GDHandle device=NULL;
  80.     LuminanceRecord LR,*LP;
  81.     ColorSpecTable *tables[TMAX];    // an array of pointers to ColorSpec tables
  82.     char string[100];
  83.     Rect r;
  84.     Boolean attenuatorInstalled;
  85.     RgnHandle rgn;
  86.     
  87.     assert(StackSpace()>16000);
  88.     #if PROFILE
  89.         InitProfile(200,3);    /* only needed if you want timing info */
  90.         _profile=0;
  91.     #endif
  92.     /* INITIALIZE QuickDraw */
  93.     MaximizeConsoleHeight();
  94.     #if (THINK_C || THINK_CPLUS)
  95.         console_options.title="\pFlickeringGrating";
  96.         console_options.nrows = 5;
  97.         printf("\n");
  98.     #elif __MWERKS__
  99.         SIOUXSettings.rows=5;
  100.         SIOUXSettings.autocloseonquit=1;
  101.         SIOUXSettings.showstatusline=0;
  102.         SIOUXSettings.asktosaveonclose=0;
  103.         printf("\n");
  104.     #else
  105.         InitGraf(&qd.thePort);
  106.         InitFonts();
  107.         InitWindows();
  108.         InitCursor();
  109.     #endif
  110.     printf("\n");    // make sure that oldPort is the console
  111.     GetPort(&oldPort);
  112.     printf("Welcome to FlickeringGrating.\n");
  113.  
  114.     for(i=8;i>=0;i--){
  115.         // look for a screen with 8-bit pixels.
  116.         device=GetScreenDevice(i);
  117.         if(device == NULL)continue;
  118.         pixelSize=(*(*device)->gdPMap)->pixelSize;
  119.         if(pixelSize==8)break;
  120.     }
  121.     if(pixelSize!=8 && NewPaletteManager())for(i=8;i>=0;i--){
  122.         // try to force a screen to 8-bit pixels.
  123.         device=GetScreenDevice(i);
  124.         if(device == NULL)continue;
  125.         SetDepth(device,8,1<<gdDevType,1);
  126.         pixelSize=(*(*device)->gdPMap)->pixelSize;
  127.         if(pixelSize==8)break;
  128.     }
  129.     if(device==NULL || pixelSize != 8) PrintfExit("Sorry, I require 8 bits/pixel.\n");
  130.     do{
  131.         if(GetScreenDevice(1)!=NULL)i=ChooseScreen(i,"Which screen?");
  132.         else i=0;
  133.         device=GetScreenDevice(i);
  134.     }while(device==NULL);
  135.     sprintf(string,"LuminanceRecord%d.h",i);
  136.     LP=&LR;
  137.     i=ReadLuminanceRecord(string,LP,0);    /* try to read correct file */
  138.     if(i<=0){
  139.         #include "LuminanceRecord1.h"
  140.     }
  141.     attenuatorInstalled=Choose(0,"Have you installed an ISR Video Attenuator?\n",noYes,2);
  142.     printf("Using luminance calibration for screen %d calibrated %s by %s.\n"
  143.         ,(int)LP->screen,LP->date,LP->notes);
  144.     if(!attenuatorInstalled){
  145.         LP->r=0.0;
  146.         LP->g=1.0;
  147.         LP->b=0.0;
  148.     }else{
  149.         // if not already in color mode, switch to color mode
  150.         if(!TestDeviceAttribute(device,gdDevType)){
  151.             error=SetDepth(device,(**(**device).gdPMap).pixelSize,1<<gdDevType,1);
  152.         }
  153.     }
  154.     window=GDOpenWindow(device);
  155.     #if PROFILE
  156.         _profile=1;
  157.     #endif
  158.  
  159.     /* load clut with linear gray scale */
  160.     // We'll leave clut entries 0 (white) and clutSize-1 (black) alone,
  161.     // since they are used heavily by Apple's stuff. Window frames
  162.     // will mostly look normal if we leave those two entries alone.
  163.     clutSize=GDClutSize(device);
  164.     SetLuminances(device,LP,1,clutSize-2,LP->LMin,LP->LMax);
  165.         
  166.     /* Display a sinusoid with a gaussian envelope */
  167.     // Compute the image.
  168.     SetPort((WindowPtr)window);
  169.     PmBackColor(1+(long)(0.5+(clutSize-3)*0.5));
  170.     EraseRect(&window->portRect);
  171.     SetRect(&r,0,0,SIZE,SIZE);
  172.     CenterRectInRect(&r,&window->portRect);
  173.     for(i=0;i<SIZE;i++){
  174.         a=(i-SIZE/2)/(SIZE/6.);
  175.         fY[i]=exp(-a*a);
  176.         fX[i]=fY[i]*sin((i-SIZE/2)*(2.0*PI/80.0));
  177.     }
  178.     for(j=0;j<SIZE;j++){
  179.         unsigned long row[SIZE];
  180.         for(i=0;i<SIZE;i++) row[i]=1+(long)(0.5+(clutSize-3)*0.5*(1.0+fY[j]*fX[i]));
  181.         SetPixelsQuickly(r.left,j+r.top,row,SIZE);
  182.     }
  183.     // These bits of conditional code force the Radius PowerView
  184.     // to update the screen from the video buffer in memory.
  185.     #if 0    // No good; causes undesired color translation.
  186.         oldDevice=GetGDevice();
  187.         SetGDevice(device);
  188.         CopyBits((BitMap *)*((CGrafPtr)window)->portPixMap
  189.             ,(BitMap *)*((CGrafPtr)window)->portPixMap
  190.             ,&r,&r,srcCopy,NULL);
  191.         SetGDevice(oldDevice);
  192.     #endif
  193.     #if 1    // Ok, but I don't actually want to scroll.
  194.         rgn=NewRgn();
  195.         ScrollRect(&r,0,1,rgn);
  196.         DisposeRgn(rgn);
  197.     #endif
  198.     // Allocate ColorSpec tables, one for each frame
  199.     for(i=0;i<TMAX;i++){
  200.         tables[i]=(ColorSpecTable *)NewPtr(sizeof(ColorSpecTable));
  201.         if(tables[i]==NULL){
  202.             //printf("StackSpace() %ld\n",StackSpace());
  203.             printf("Only room for %d lookup tables (one per frame) ... continuing.\n",i);
  204.             break;
  205.         }
  206.     }
  207.     tmax=i;
  208.     // Compute lookup tables.
  209.     LMid=(LP->LMax+LP->LMin)/2.0;
  210.     contrast=(LP->LMax-LP->LMin)/(LP->LMax+LP->LMin);
  211.     LMax=LMid*(1.0+contrast);
  212.     LMin=LMid*(1.0-contrast);
  213.     for(i=0;i<tmax;i++){
  214.         a=6.0*(i-tmax/2)/tmax;
  215.         dL=LMid*contrast*exp(-a*a)*sin((i-tmax/2)*(2.0*PI*3.0*0.015));
  216.         SetLuminancesAndRange(NULL,LP,1,clutSize-2,LMid-dL,LMid+dL,LMin,LMax);
  217.         *tables[i]= *(ColorSpecTable *)LP->table;
  218.     }
  219.     printf("Now displaying the animation ...\n");
  220.     for(j=0;j<REPETITIONS;j++){
  221.         for(i=0;i<tmax;i++){
  222.             // This is a thinly disguished call to GDSetEntries.
  223.             LoadLuminances(device,(LuminanceRecord *) tables[i],1,clutSize-2);
  224.         }
  225.     }
  226.     #if PROFILE
  227.         _profile=0;
  228.     #endif
  229.     for(i=0;i<tmax;i++)DisposPtr((Ptr)tables[i]);
  230.     SetPort(oldPort);
  231.     GDDisposeWindow(window);
  232.     RestoreCluts();
  233.     #if !PROFILE
  234.         abort();
  235.     #endif
  236. }
  237.